<?
if(!defined("B_PROLOG_INCLUDED")||B_PROLOG_INCLUDED!==true)die();

if (!CModule::IncludeModule("consult"))
	return;
	
$arResult["AUTHOR"] = false;
$arResult["ERRORS"] = array();
$arResult["NEED_REGISTER"] = false;
$bAuth = $USER->IsAuthorized();

$arParams["USE_CAPTCHA"] = (($arParams["USE_CAPTCHA"] != "N" && !$USER->IsAuthorized()) ? "Y" : "N");

$arResult["URL_CHAT"] = trim ($arParams["URL_CHAT"]);
if (strlen ($arResult["URL_CHAT"]) <= 0)
	$arResult["URL_CHAT"] = $APPLICATION->GetCurPage();

$arParams["DEF_NAME"] = (strlen($arParams["DEF_NAME"]) > 0) ? $arParams["DEF_NAME"] : "Anonimus";

///////////////////
// GET USER INFO //
///////////////////

$obConsUser = new CConsultUser;
	$arResult["AUTHOR"] = $obConsUser->GetCurConsultUser(true);

///////////////
// POST WORK //
///////////////
if($_SERVER["REQUEST_METHOD"] == "POST" && strlen($_POST["submit"]) > 0 && check_bitrix_sessid())
{

	if (!$arResult["AUTHOR"] && !isset($_POST["remove"]))
	{
		//////////////////////////////////
		// CHECK CAPTCHA, WHEN REGISTER //
		//////////////////////////////////
		if($arParams["USE_CAPTCHA"] == "Y")
		{
			include_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/classes/general/captcha.php");
			$captcha_code = $_POST["captcha_sid"];
			$captcha_word = $_POST["captcha_word"];
			$cpt = new CCaptcha();
			$captchaPass = COption::GetOptionString("main", "captcha_password", "");

			if (strlen($captcha_word) > 0 && strlen($captcha_code) > 0)
			{
				if (!$cpt->CheckCodeCrypt($captcha_word, $captcha_code, $captchaPass))
					$arResult["ERRORS"][] = "Wrong captcha!";
			}
			else
				$arResult["ERRORS"][] = "Captcha field is empty!";
		}
	}

	if ($arResult["AUTHOR"] && isset($_POST["remove"]))
	{
		////////////////////
		// DELETE PROFILE //
		////////////////////
		if ($obConsUser->Delete ($arResult["AUTHOR"]["ID"]))
			$arResult["AUTHOR"] = false;
	}

	if (count ($arResult["ERRORS"]) <= 0)
	{
		////////////////////
		// UPDATE PROFILE //
		////////////////////
		if ($arResult["AUTHOR"])
		{
			$user_name = trim ($_POST["cons_name"]);
			if (strlen ($user_name) > 0 && $user_name != $arResult["AUTHOR"]["NAME"])
			{
				$obConsUser->Update($arResult["AUTHOR"]["ID"], array("NAME"=> $user_name));
			}
			$arResult["TARGET_PAGE"] = $arResult["URL_CHAT"]."?author_id=".$arResult["AUTHOR"]["ID"];
			LocalRedirect ($arResult["TARGET_PAGE"]);
		}
		elseif(!$arResult["AUTHOR"])
		{
			/////////////////////
			// ADD NEW PROFILE //
			/////////////////////
			if ($bAuth)
				$arUserAdd = array("ENTITY_TYPE"=> "U", "ENTITY_ID" => $USER->GetID());
			else
			{
				$arUserAdd = array("ENTITY_TYPE"=>"G", "ENTITY_ID"=>bitrix_sessid());
			}
			$cons_name = trim($_POST["cons_name"]);
			$arUserAdd["NAME"] = (strlen ($cons_name) > 0)? $cons_name : $arParams["DEF_NAME"];

			$ConsUser = new CConsultUser;
			$arParams["author_id"] = $ConsUser->Add($arUserAdd);

			if ($arParams["author_id"])
			{
				$APPLICATION->set_cookie("CONSULT_USER_SID", $arUserAdd["ENTITY_ID"]);
				//Создаём топик
				//Надо найти ответственного!!!
				$arResponsible = CGroup::GetGroupUser(1);
				$resp = $arResponsible[array_rand($arResponsible)];
				$arFields = array(
					"RESPONSIBLE_ID" => $resp,
					"OWNER_ID" => $arParams["author_id"], //АВТОР!!!!
				);
				$ob_stream = new CConsultStream;

				$arParams["stream_id"] = $ob_stream->Add($arFields);
				if ($arParams["stream_id"])
				{
					$arResult["TARGET_PAGE"] = $arResult["URL_CHAT"]."?stream_id=".$arParams["stream_id"]."&author_id=".$arParams["author_id"];
					LocalRedirect ($arResult["TARGET_PAGE"]);
				}
				else
					$arResult["ERRORS"][] = $ob_stream->LAST_ERROR;
			}
			else
				$arResult["ERRORS"][] = $ConsUser->LAST_ERROR;
			/////////////////////////
			// END ADD NEW PROFILE //
			/////////////////////////
		}
	}
}

///////////////////////
// WORK FOR TEMPLATE //
///////////////////////
if (!$arResult["AUTHOR"])
{
	$arParams["NEED_REGISTER"] = true;

	if ($arParams["USE_CAPTCHA"] == "Y")
		$arParams["CAPTCHA_CODE"] =  htmlspecialchars($APPLICATION->CaptchaGetCode());
}

//////////////////////
// INCLUDE TEMPLATE //
//////////////////////

$this->IncludeComponentTemplate();
?>